/* eslint-disable curly */
/* eslint-disable indent */
import Loading from './src/loading'
import common from '../../until/common'

let loadingInstance
let propsDefault = {
    // 主题
    theme: 'default',
    // icon图标
    icon: 'ios-loading',
    // 高度
    height: null,
    // 背景色
    background: null,
    // 文字颜色
    color: null,
    // 进度条颜色
    progressColor: null,
    // 是否隐藏进度值
    disProgressText: false,
    // 百分比颜色
    percentColor: null,
    // 失败颜色
    errorColor: '#FF0000',
    // 层级
    zIndex: null,
    // 提示文字
    content: '加载中，请耐心等待',
    // 隐藏时的持续时间，单位 ms
    duration: 800
}
let props = common.deepCopy(propsDefault)
let timer

const getLoadingInstance = () => {
    loadingInstance = loadingInstance || Loading.instance(props)
    return loadingInstance
}

const update = options => {
    let instance = getLoadingInstance()
    instance.update(options)
}

const hide = () => {
    let timerHide = setTimeout(() => {
        update({ show: false })
        clearTimeout(timerHide)
        let timerPercent = setTimeout(() => {
            update({ percent: 0 })
            clearTimeout(timerPercent)
        }, 200)
    }, props.duration)
}

let clearTimer = () => {
    if (timer) {
        clearInterval(timer)
        timer = null
    }
}

// 全局配置
Loading.config = (options) => {
    props = common.extend(props, options, true)
}

// 全局销毁
Loading.destroy = () => {
    let instance = getLoadingInstance()
    loadingInstance = null
    props = common.deepCopy(propsDefault)
    instance.destroy()
}

// 精确加载到指定的进度
Loading.update = (percent) => {
    switch (props.theme) {
        case 'default':
        case 'box':
        case 'icon':
            update({ show: true })
            break
        case 'progress':
        case 'percent':
            clearTimer()
            update({ percent: percent, status: 'success', show: true })
            break
        default:
            update({ show: true })
            break
    }
}

// 开始从 0 显示进度条，并自动加载进度
Loading.start = () => {
    switch (props.theme) {
        case 'default':
        case 'box':
        case 'icon':
            update({ show: true })
            break
        case 'progress':
        case 'percent':
            if (timer) return
            let percent = 0
            update({ percent: percent, status: 'success', show: true })
            timer = setInterval(() => {
                percent += Math.floor(Math.random() * 3 + 1)
                if (percent > 95) {
                    clearTimer()
                }
                update({ percent: percent, status: 'success', show: true })
            }, 200)
            break
        default:
            update({ show: true })
            break
    }
}

// 结束进度条，自动补全剩余进度
Loading.finish = () => {
    switch (props.theme) {
        case 'default':
        case 'box':
        case 'icon':
            update({ show: false })
            break
        case 'progress':
        case 'percent':
            clearTimer()
            update({ percent: 100, status: 'success', show: true })
            hide()
            break
        default:
            update({ show: false })
            break
    }
}

// 以错误的类型结束进度条，自动补全剩余进度
Loading.error = () => {
    switch (props.theme) {
        case 'default':
        case 'box':
        case 'icon':
            update({ show: false })
            break
        case 'progress':
        case 'percent':
            clearTimer()
            update({ percent: 100, status: 'error', show: true })
            hide()
            break
        default:
            update({ show: false })
            break
    }
}

Loading.install = function(Vue) {
    Vue.component(Loading.name, Loading)
}
export default Loading